;---------------------------------------------------------------------
;
; File: example_b.sexp
; Basic arithmetic operations that demonstrate the use of recursion
; in SIF01.
;
; Copyright (C) 2006-2008 by Ariel Ortiz <ariel.ortiz@itesm.mx>
;
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;
;---------------------------------------------------------------------

(define zero? null?)

(define add1
  (fn (n)
    (cons () n)))

(define sub1 rest)

(define zero  ())
(define one   (add1 zero))
(define two   (add1 one))
(define three (add1 two))
(define four  (add1 three))
(define five  (add1 four))

(define add
  (fn (a b)
    (if (zero? a)
        b
        (add1 (add (sub1 a) b)))))

(define sub
  (fn (a b)
    (if (zero? b)
        a
        (sub1 (sub a (sub1 b))))))

(define mul
  (fn (a b)
    (if (zero? a)
        a
        (add b (mul (sub1 a) b)))))

(define equal?
  (fn (a b)
    (if (zero? a)
        (if (zero? b)
            (quote t)
            ())
        (if (zero? b)
            ()
            (equal? (sub1 a) (sub1 b))))))

(define less?
  (fn (a b)
    (if (zero? a)
        (if (zero? b)
            ()
            (quote t))
        (if (zero? b)
            ()
            (less? (sub1 a) (sub1 b))))))

(define div
  (fn (a b)
    (if (less? a b)
        zero
        (add1 (div (sub a b) b)))))

(define mod
  (fn (a b)
    (if (less? a b)
        a
        (mod (sub a b) b))))

(define pow
  (fn (a b)
    (if (zero? b)
        one
        (mul a (pow a (sub1 b))))))

                   ; Displayed results:
                   ;
(add two three)    ;         (() () () () ())
(sub five two)     ;         (() () ())
(mul two three)    ;         (() () () () () ())
(equal? two two)   ;         t
(equal? two three) ;         ()
(less? two two)    ;         ()
(less? two three)  ;         t
(div five two)     ;         (() ())
(mod five two)     ;         (())
(pow two three)    ;         (() () () () () () () ())
